<script setup>
import { ref } from 'vue'
import { request } from '@/request/request'
import { onLaunch, onLoad } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
const dis_code = ref('');

onLoad((options) => {
  console.log(options);
  uni.setStorageSync('aichat_dis_code', options.dis_code);
  dis_code.value = options.dis_code;
})

onLaunch(() => {
  uni.login({
    provider: 'weixin',
    success: (res) => {
      console.log(res.code);
      const code = res.code;
      uni.setStorageSync('aichat_code', code);
      request.post('user/login', { code: code, dis_code: dis_code.value }, false, true).then((res) => {
        if (res.code === 200) {
          uni.setStorageSync('aichat_user', res.data);
          uni.setStorageSync('aichat_token', res.data.token);
        } else {
          console.log('登录失败：', res);
          uni.showToast({
            title: res.message || t('login.failed'),
            icon: 'none'
          });
        }
      }).catch((err) => {
        console.error('登录错误：', err);
      })
    },
    fail: (err) => {
      console.error('微信登录失败：', err);
      uni.showToast({
        title: t('login.wechatFailed'),
        icon: 'none'
      });
    }
  })
})
</script>

<style lang="scss">
@import '@tuniao/tn-style/dist/uniapp/index.css';

@font-face {
  font-family: 'UKIJ';
  src: url('https://ai.xzzjyzx.com/static/font/UKIJTor.ttf');
  font-weight: normal;
  font-style: normal;
}

page {
  font-family: 'UKIJ';
}
</style>
